package item.inkcloud.communityservice.service;


import item.inkcloud.communityservice.entity.Article;
import item.inkcloud.communityservice.feignClient.UserServiceFeignClient;
import item.inkcloud.communityservice.mapper.ArticleMapper;
import item.inkcloud.communityservice.mapper.CommentMapper;
import item.inkcloud.communityservice.pojo.ArticleUtils;
import item.inkcloud.communityservice.pojo.BriefArticleInfo;
import item.inkcloud.communityservice.pojo.BriefCommentInfo;
import item.inkcloud.communityservice.pojo.DetailedArticleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;


@Service
public class ArticleService {

    @Autowired
    UserServiceFeignClient userServiceFeignClient;

    @Autowired
    CommentMapper commentMapper;

    @Autowired
    ArticleMapper articleMapper;

    public int publishArticle(Article article,String userid){

        String author = userServiceFeignClient.getUsernameById(userid);

        article.setAuthor(author);

        article.setSubmitTime(ArticleUtils.getCurrentDate());

        String articleId = ArticleUtils.getArticleId(article.getAuthor());

        article.setArticleId(articleId);

        try {
            articleMapper.publishArticle(article);
            return 1;
        }catch (Exception e){

            System.out.println(e.toString()+e.getMessage());
            return 0;
        }

    }


    public ArrayList<BriefArticleInfo> getArticleList(){

        ArrayList<BriefArticleInfo> infos = new ArrayList<>();

        try {
            infos = articleMapper.getAllBrief();
            return infos;

        }catch (Exception e){

            infos = null;
            return infos;

        }

    }



    public DetailedArticleInfo getArticleDetail(String articleId){

        DetailedArticleInfo info = new DetailedArticleInfo();

        try {
            info = articleMapper.findArticleById(articleId);
            ArrayList<BriefCommentInfo> comment = commentMapper.findCommentById(articleId);
            info.setComment(comment);
            return info;
        }catch (Exception e){

            info = null ;
            return info;
        }


    }







}
